package xjtu.sad.zbsj.user.pesonal;


import xjtu.sad.zbsj.common.CommonService;
import xjtu.sad.zbsj.common.CurrentUser;
import xjtu.sad.zbsj.common.Result;
import xjtu.sad.zbsj.common.TokenUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author lkm
 */
@RestController
@RequestMapping("/user/personal")
public class PersonalAPI {
    @Resource
    private PersonalService personalService;
    @Resource
    private CommonService commonService;

    @RequestMapping("")
    public Result geUserInfo(String token) {
        CurrentUser currentUser = TokenUtils.getUserInfo(token, commonService);
        System.out.println(currentUser.getU_id());
        String userId = currentUser.getU_id();
//        String userId = "U202111060001";
        Personal currUserInfo = personalService.getPersonalByU_id(userId);
        return Result.success("获取用户信息成功", currUserInfo);
    }


    @RequestMapping("/update")
    public Result updatePersonalInfo(@RequestBody PersonalDto personalDto) {
        System.out.println(personalDto.getU_id());
        String u_id = TokenUtils.getUserInfo(personalDto.getToken(), commonService).getU_id();
        personalDto.setU_id(u_id);
        personalService.updatePersonalInfo(personalDto);
        return Result.success("更新用户信息成功", personalDto);
    }

}
